package com.welkom.everydate.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.welkom.everydate.common.Constants;
import com.welkom.everydate.dao.ResourceDao;
import com.welkom.everydate.services.interfaces.ResourceService;
import com.welkom.everydate.util.MessageReader;
import com.welkom.everydate.util.web.Operation;
import com.welkom.everydate.util.web.PageData;
import com.welkom.everydate.util.web.PageHeaderAnnotationResolver;
import com.welkom.everydate.util.web.RequestResult;
@Controller
public class ResourceManagementController {
	@Resource
	private ResourceDao resourceDao;
	@Resource
	private ResourceService resourceService;
	@RequestMapping("/resourceManagement.html")
	public String resourceManagement(Model model){
		return "system/resourceManagement";
	}
	
	 @RequestMapping("/getResources.html")
	 @ResponseBody
	 public PageData getResourcesByPage(@RequestParam(required = true, value = "pageNum") int pageNum ,@RequestParam( required=true,value="pageSize") int pageSize,
				Model model,HttpServletRequest request){
		 PageData<com.welkom.everydate.domain.Resource> page=new PageData<com.welkom.everydate.domain.Resource>();
		 int totalCount=resourceDao.getReourceTotalCount();
		 page.setTotalCount(totalCount);
		 page.setPageSize(pageSize);
		 page.setCurrentPage(pageNum);
		 int startIndex=page.getStartIndex();
		 if(pageSize==0){
			pageSize=Constants.Pagination.DEFAULT_PAGE_SIZE;
		 }
		 List<com.welkom.everydate.domain.Resource> resources=resourceDao.getAllResourcesByPage(startIndex, pageSize);
		 page.setData(resources);
		 if(page.getPageNavigatorLength()==0){
			 page.setPageNavigatorLength(Constants.Pagination.DEFAULT_PAGE_NAVIGATOR_LENGTH);
		 }
		 page.setPageHeaders(PageHeaderAnnotationResolver.getHeadersFromDomain(com.welkom.everydate.domain.Resource.class));
		 List<Operation> operations=new ArrayList<Operation>();
		 page.setOperations(operations);
		 request.getSession().setAttribute(Constants.SessionPageData.SESSION_PAGE_DATE_RESOURCE,page);
		 return page;
	 }
	 @RequestMapping("/getResourceDetail.html")
	 @ResponseBody
	 public RequestResult<com.welkom.everydate.domain.Resource> getResourceDetail(@RequestParam(required=true,value="resourceId")long resourceId){
		 RequestResult<com.welkom.everydate.domain.Resource> result=new RequestResult<com.welkom.everydate.domain.Resource>();
		 com.welkom.everydate.domain.Resource resource=resourceDao.getResourceById(resourceId);
		 if(resource==null){
			 result.setResultCode(Constants.ResultCode.RESULT_CODE_FAIL);
			 result.setReason(MessageReader.getMessage(Constants.Message.MESSAGE_OBJECT_NOT_EXSIT));
		 }else{
			 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
			 result.setData(resource);
		 }
		 return result;
	 }
	 
	 @RequestMapping("/saveChangesToResource.html")
	 @ResponseBody
	 public RequestResult<com.welkom.everydate.domain.Resource> saveChanesToResource(@RequestParam(required=true,value="resourceInfo") String resourceInfo){
		 JSONArray array = JSONArray.fromObject(resourceInfo);//先读取串数组
		 Object[] objectArray = array.toArray();   
		 JSONObject obj = JSONObject.fromObject(objectArray[0]);
		 com.welkom.everydate.domain.Resource resource=(com.welkom.everydate.domain.Resource)obj.toBean(obj, com.welkom.everydate.domain.Resource.class);
		 resourceService.saveChangesToResource(resource);
		 RequestResult<com.welkom.everydate.domain.Resource> result=new RequestResult<com.welkom.everydate.domain.Resource>();
		 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		 return result;	 
	 }
	 @RequestMapping("/deleteResoureById.html")
	 @ResponseBody
	 public RequestResult<com.welkom.everydate.domain.Resource> deleteResoureById(@RequestParam(required=true,value="resourceId") long resourceId){
		 resourceService.removeResource(resourceId);
		 RequestResult<com.welkom.everydate.domain.Resource> result=new RequestResult<com.welkom.everydate.domain.Resource>();
		 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		 return result;	 
	 }
	 
	 @RequestMapping("/addResource.html")
	 @ResponseBody
	 public RequestResult<com.welkom.everydate.domain.Resource> addResource(@RequestParam(required=true,value="resourceInfo") String resourceInfo){
		 JSONArray array = JSONArray.fromObject(resourceInfo);//先读取串数组
		 Object[] objectArray = array.toArray();   
		 JSONObject obj = JSONObject.fromObject(objectArray[0]);
		 com.welkom.everydate.domain.Resource resource=(com.welkom.everydate.domain.Resource)obj.toBean(obj, com.welkom.everydate.domain.Resource.class);
		 resourceService.addResource(resource);
		 RequestResult<com.welkom.everydate.domain.Resource> result=new RequestResult<com.welkom.everydate.domain.Resource>();
		 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		 return result;	 
	 }
	 
}
